package top.eggcode.modules.dict.service;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.eggcode.component.mds.DsSwitch;
import top.eggcode.component.mds.Source;
import top.eggcode.component.query.model.PageList;
import top.eggcode.modules.dict.model.WordEntity;
import top.eggcode.modules.dict.model.WordQuery;
import top.eggcode.modules.dict.store.WordRepository;
import top.eggcode.modules.dict.store.WordStore;
import top.eggcode.base.BaseMarkServiceImpl;

/**
 * Title:
 * Description: TODO
 * Date: 2021/5/21 20:46
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Slf4j
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
@Service
public class WordService extends BaseMarkServiceImpl<WordEntity> {

    private final WordRepository selfRepository;

    private final WordStore selfAccessor;

    @Override
    public boolean add(WordEntity entity) {
        Integer maxSequence = selfRepository.selectMaxSequence(entity.getDictId());
        if (maxSequence == null) {
            maxSequence = 0;
        }
        if (entity.getSequence() == null || entity.getSequence() <= maxSequence) {
            entity.setSequence(maxSequence + 1);
        }
        return super.add(entity);
    }

    @DsSwitch(Source.SLAVE)
    public PageList<WordEntity> page(WordQuery query) {
        return selfAccessor.page(query);
    }

}
